#!/bin/bash
#
##############################################################################
#
# Copyright (c) 2008, League of Crafty Programmers Ltd <info@locp.co.uk>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY LEAGUE OF CRAFTY PROGRAMMERS ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL LEAGUE OF CRAFTY PROGRAMMERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##############################################################################
#

function end_xml_report {
	TIME=$( date +%s )
	TIMESTR=$( date )

	cat <<_EOF
<runstats><finished time="${TIME}" timestr="${TIMESTR}"/><hosts up="${hosts_up}" down="0" total="${hosts_up}" />
</runstats></nmaprun>
_EOF
}

function start_xml_report {
	START=$( date +%s )
	STARTSTR=$( date )

	cat <<_EOF
<?xml version="1.0" ?>
<?xml-stylesheet href="nmap.xsl" type="text/xsl"?>
<nmaprun scanner="nmap" args="nmap -sS -sU -A --stylesheet nmap.xsl -oX scan.xml 192.168.0.0/16" start="${START}" startstr="${STARTSTR}" version="4.11" xmloutputversion="1.01">
<scaninfo type="syn" protocol="tcp" numservices="1680" services="1-1027,1029-1033,1040,1043,1050,1058-1059,1067-1068,1076,1080,1083-1084,1103,1109-1110,1112,1127,1139,1155,1158,1178,1212,1214,1220,1222,1234,1241,1248,1337,1346-1381,1383-1552,1600,1650-1652,1661-1672,1680,1720,1723,1755,1761-1764,1827,1900,1935,1984,1986-2028,2030,2032-2035,2038,2040-2049,2053,2064-2065,2067-2068,2105-2106,2108,2111-2112,2120-2121,2201,2232,2241,2301,2307,2401,2430-2433,2500-2501,2564,2600-2605,2627-2628,2638,2766,2784,2809,2903,2998,3000-3001,3005-3006,3049,3052,3064,3086,3128,3141,3264,3268-3269,3292,3306,3333,3372,3389,3421,3455-3457,3462,3531,3632,3689,3900,3984-3986,3999-4000,4002,4008,4045,4125,4132-4133,4144,4224,4321,4333,4343,4444,4480,4500,4557,4559,4660,4662,4672,4899,4987,4998,5000-5003,5010-5011,5050,5060,5100-5102,5145,5190-5193,5232,5236,5300-5305,5308,5400,5405,5432,5490,5510,5520,5530,5540,5550,5555,5560,5631-5632,5679-5680,5713-5717,5800-5803,5900-5903,5977-5979,5997-6009,6017,6050,6101,6103,6105-6106,6110-6112,6141-6148,6346-6347,6400-6401,6502,6543-6544,6547-6548,6558,6588,6666-6668,6699,6881,6969,7000-7010,7070,7100,7200-7201,7273,7326,7464,7597,7937-7938,8000,8007,8009,8021,8080-8082,8443,8888,8892,9090,9100,9111,9152,9535,9876,9991-9992,9999-10000,10005,10082-10083,11371,12000,12345-12346,13701-13702,13705-13706,13708-13718,13720-13722,13782-13783,15126,16444,16959,17007,17300,18000,18181-18185,18187,19150,20005,22273,22289,22305,22321,22370,26208,27000-27010,27374,27665,31337,31416,32770-32780,32786-32787,38037,38292,43188,44334,44442-44443,47557,49400,50000,50002,54320,61439-61441,65301" />
<scaninfo type="udp" protocol="udp" numservices="1487" services="1-1025,1028,1030-1032,1034,1043,1058-1059,1067-1068,1080,1083-1084,1110,1155,1167,1212,1214,1222,1248,1346-1381,1383-1552,1600,1645-1646,1650-1652,1661-1672,1701,1812-1813,1900,1986-2002,2004-2028,2030,2032-2035,2038,2040-2049,2065,2067,2103-2106,2108,2201,2232,2241,2307,2401,2430-2433,2500-2501,2627,2784,2948,2967,3049,3130,3141,3246,3264,3333,3401,3421,3455-3457,3531,3900,3984-3986,3996-3998,4000,4008,4045,4132-4133,4321,4343,4444,4500,4666,4672,4827,5000-5003,5010-5011,5050,5060,5145,5190-5193,5236,5300-5305,5308,5428,5500,5540,5555,5632,5713-5717,6110-6111,6141-6148,6346-6347,6502,6549,6558,6969,7000-7010,7100,7200-7201,7648-7651,9535,9876,10080,16444,17007,17185,18000,22370,26000,26900,27015,27444,27500,27910,27960,28910,31335,31337,32768,32770-32780,32786-32787,38037,38293,39213,45000,47557,54321" />
<verbose level="0" />
<debugging level="0" />
_EOF
}

if [ $# != 1 ]; then
	echo "usage: $( basename $0 ) file.csv" >&2
	exit 1
fi

start_xml_report
hosts_up=0

for hostrec in $( cat $1 ); do
	HOST=$( echo $hostrec | cut -d, -f1 )
	IP=$( echo $hostrec | cut -d, -f2 )
	template=$( echo $hostrec | cut -d, -f3 )
	MAC=$( echo $hostrec | cut -d, -f4 )

	if [ ! -f $template ]; then
		echo "<!-"
		echo "Missing template file $template $HOST ($IP) - $MAC"
		echo "-->"
	else
		sed s/IP/$IP/g $template | \
		    sed s/HOST/$HOST/g | \
		    sed s/MAC/$MAC/g
	fi

	(( hosts_up += 1 ))
done

end_xml_report 
